package com.mzw.huaxiarealestatebackend.controller;
import com.mzw.huaxiarealestatebackend.common.utils.DataResult;
import com.mzw.huaxiarealestatebackend.entity.BackPage;
import com.mzw.huaxiarealestatebackend.entity.Client;
import com.mzw.huaxiarealestatebackend.entity.Department;
import com.mzw.huaxiarealestatebackend.entity.Result;
import com.mzw.huaxiarealestatebackend.service.DepartmentService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Department)表控制层
 *
 * @author makejava
 * @since 2023-12-11 16:21:55
 */
@RestController
@RequestMapping("department")
public class DepartmentController {
    /**
     * 服务对象
     */
    @Resource
    private DepartmentService departmentService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public DataResult queryAll(){
        List<Department> result = departmentService.queryAll();
        //判断是否有数据
        boolean isEmpty = result.isEmpty();
        return new DataResult(isEmpty ? 403 : 200, isEmpty ? "查询失败" : "查询成功", result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public DataResult queryById(@PathVariable("id") String id) {
        Department department = this.departmentService.queryById(id);
        if (department != null){
            return DataResult.ok(department);
        }else {
            return DataResult.failure("查找失败");
        }
    }

    /**
     * 新增数据
     *
     * @param department 实体
     * @return 新增结果
     */
    @PostMapping
    public DataResult add(@RequestBody Department department) {
        boolean insert = this.departmentService.insert(department);
        if (insert){
            return DataResult.ok();
        }else {
            return DataResult.failure("添加失败");
        }
    }

    /**
     * 编辑数据
     *
     * @param department 实体
     * @return 编辑结果
     */
    @PutMapping
    public DataResult edit(@RequestBody Department department) {
        boolean update = this.departmentService.update(department);
        if (update){
            return DataResult.ok();
        }else {
            return DataResult.failure("修改失败");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public DataResult deleteById(@RequestParam("id") String id) {
        boolean b = this.departmentService.deleteById(id);
        if (b){
            return DataResult.ok();
        }else {
            return DataResult.failure("删除失败");
        }
    }

}

